import 'package:flutter/services.dart';
import 'package:lib_service/cache_service.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

class CacheServiceImpl implements CacheService {
  final MethodChannel _channel = const MethodChannel('cache-service');

  CacheServiceImpl() {
    _channel.setMethodCallHandler(handler);
  }

  @override
  Future<void> clearCache() async {
    return DefaultCacheManager().emptyCache();
  }

  Future<dynamic> handler(call) async {
    switch (call.method) {
      case 'clearCache':
        return clearCache();

      default:
        return false;
    }
  }

  @override
  void setInt({
    required String key,
    required int value,
    String cacheType = "user",
  }) {
    _channel.invokeMethod('setInt', {"key": key, "value": value});
  }

  @override
  Future<int?> getInt({
    required String key,
    String cacheType = "user",
  }) {
    return _channel.invokeMethod<int>('getInt', {"key": key});
  }
}
